//
//  MovieDetailView.m
//  TableView
//
//  Created by Carl_fang on 15/3/26.
//  Copyright (c) 2015年 Carl_fang. All rights reserved.
//

#import "MovieDetailView.h"
#import "MovieCell.h"
#import "JuQingTableViewCell.h"
#import "DetailModel.h"
#import "PicTableViewCell.h"
#import "PicModel.h"
#import "UIImageView+WebCache.h"
#import "PicControllView.h"
#import "movieImageModel.h"



@interface MovieDetailView() <NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
    NSURLConnection *urlConnection;
}
@property (nonatomic, retain) NSMutableData *recevieData;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *PicModelArray;
@property (nonatomic, retain) DetailModel *detailModel;
@property (nonatomic) BOOL finished;
@property (nonatomic) CGFloat height;


@end


@implementation MovieDetailView

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.tableView.tableFooterView = [UIView new];
    //    self.recevieData = [NSMutableData data];
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    
    NSString *urlStr = [NSString stringWithFormat:@"http://api.m.mtime.cn/Showtime/MovieDetail.api?locationId=292&movieId=%@", self.movieID];
    NSLog(@"%@",urlStr);
    NSURL *url = [[NSURL alloc] initWithString:urlStr];
    //NSLog(@"%@",urlStr);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
    //    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url] options:NSJSONReadingMutableLeaves error:nil];
    //
    //    self.detailModel = [DetailModel DetailWithDict:dict];
    //    NSArray *array = dict[@"actorimg"];
    //
    //    for (NSDictionary *dic in array) {
    //
    //        PicModel *picModel = [PicModel picModelWithDict:dic];
    //        [_PicModelArray addObject:picModel.image];
    //    }
   urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
    
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recevieData appendData:data];
    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.PicModelArray = [NSMutableArray array];
    self.recevieData = [NSMutableData data];
    self.array = [NSMutableArray array];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.recevieData options:NSJSONReadingMutableLeaves error:nil];
    
    self.detailModel = [DetailModel DetailWithDict:dict];
//    NSArray *array = dict[@"movieimg"];

    //NSLog(@"%@",dict);
    for (NSDictionary *dic in [dict objectForKey:@"movieimg"]) {
        
        PicModel *picModel = [PicModel picModelWithDict:dic];
        [_PicModelArray addObject:picModel.image];
    }
    for (NSDictionary *dic in dict[@"actorimg"]) {
        
        movieImageModel *model = [movieImageModel movieImageWithDict:dic];
        [_array addObject:model];
        
    }

    if (_PicModelArray.count != 0 && _array.count != 0) {
        //NSLog(@"请求成功%d",_PicModelArray.count);
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView reloadData];

    }else
    {
        NSLog(@"请求失败%d",_PicModelArray.count);
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView reloadData];
    }
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 200;
            break;
            case 1:
            if (_height == 0) {
                return 120;
            }else
            {
                return _height + 15;

            }
            break;
            
            case 2:
            
            return 120;
            
            break;
        default:
            return 50;
            break;
    }
    
  }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *ID = @"cell";
    static NSString *Movie = @"movieCell";
    
    static NSString *Juqing = @"Juqing";
    static NSString *Pic = @"Pic";
    
    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0) {
                MovieCell *cell = [[MovieCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Movie];
                if (cell == nil) {
                    cell = [[MovieCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Movie];
                }
                
                
                
                cell.detailModel = self.detailModel;

                return cell;
            }else if (indexPath.row == 1)
            {
                JuQingTableViewCell *cell = [[JuQingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Juqing];
                if (cell == nil) {
                    cell = [[JuQingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Juqing];
                }
                cell.detailModel = _detailModel;
                
            
                //NSLog(@"%@",cell);
                [cell.allTextBtn addTarget:self action:@selector(reload:) forControlEvents:UIControlEventTouchUpInside];
                
                return cell;
            }else if (indexPath.row == 2)
            {
                
                PicTableViewCell *cell = [[PicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Pic];
                if (cell == nil)
                {
                    cell = [[PicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Pic];
                }
                cell.contentMode = UIViewContentModeScaleAspectFit;
                //NSLog(@"%d",_PicModelArray.count);
                [cell setDetailArray:_PicModelArray detailMode:_detailModel];
                return cell;
                
            }
            
        case 1:
            {
                
                
            }
            
        default :
            {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
                
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
                }
                
                return cell;
                
            }
            
        }
            
            
    }
    
    
    
}


- (void)reload:(UIButton *)btn
{
    
    _height = [JuQingTableViewCell heightForString:_detailModel.pt];
    [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:NO];
    
//    self tableView 
    
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    //[center postNotificationName:@"btn" object:nil];
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:_height], @"height", nil];
    [center postNotificationName:@"btn" object:nil userInfo:dic];
    
//    JuQingTableViewCell *j = [JuQingTableViewCell alloc]
//    btn.superview.frame = CGRectMake(10, 10, 280, _height);
//    btn.superview.subviews[0]
//    frame = CGRectMake(10, 10, 280, _height);
    
    
    NSLog(@"%s",__func__);
    //[self.tableView reloadData];
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
            
        case 0:
        {
            if (indexPath.row == 2) {
                
                
                UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
                PicControllView *picVC = [[PicControllView alloc]initWithCollectionViewLayout:layout];
                
                picVC.picArray = _array;
        
                [self.navigationController pushViewController:picVC animated:YES];
            }
            
            
        }
            
    }
}


@end
